package utils

import (
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"regexp"
	"strings"
)

func TranslatorError(err error) string {
	reg := regexp.MustCompile(`"(.*)?"`)
	match := reg.FindString(err.Error())
	if strings.Contains(err.Error(), "is not set") {
		if match != "" {
			return fmt.Sprintf("%s 为必填字段!", strings.Replace(match, `"`, "", -1))
		}
	} else if strings.Contains(err.Error(), "mismatch for field") {
		if match != "" {
			return fmt.Sprintf("%s 数据类型不对!", strings.Replace(match, `"`, "", -1))
		}
	} else if strings.Contains(err.Error(), "record not found") {
		return "未找到符合条件的记录"
	} else if strings.Contains(err.Error(), "transaction has already been committed or rolled back") {
		logx.Errorf("执行事务操作错误:%v", err)
		return "操作失败"
	}
	return ""
}
